package com.explorx.sns.puma.im.core.chat.impl;

import com.explorx.sns.puma.im.core.chat.model.PrivateMessageModel;
import com.explorx.sns.puma.im.core.chat.PrivateMessageService;
import com.explorx.sns.puma.im.core.dal.dao.PrivateMessageEntityMapper;
import com.explorx.sns.puma.im.core.dal.entity.PrivateMessageEntity;
import com.explorx.sns.puma.im.core.dal.entity.PrivateMessageEntityExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PrivateMessageServiceImpl implements PrivateMessageService {

    @Resource
    private PrivateMessageEntityMapper privateMessageEntityMapper;

    @Override
    public boolean deleteMessage(PrivateMessageModel privateMessageModel) {
        PrivateMessageEntity entity=new PrivateMessageEntity();
        entity.setMessageId(privateMessageModel.getMessageId());
        entity.setCid(privateMessageModel.getCid());
        entity.setStatus((byte) privateMessageModel.getStatus());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setSenderId(privateMessageModel.getSenderId());
        return privateMessageEntityMapper.insert(entity)>0;
    }

    @Override
    public List<PrivateMessageModel> queryPrivateMessageByIds(Long uid, Long cid, List<Long> msgIds) {
        PrivateMessageEntityExample example=new PrivateMessageEntityExample();
        example.createCriteria().andCidEqualTo(cid).andSenderIdEqualTo(uid);
        final List<PrivateMessageEntity> privateMessageEntities = privateMessageEntityMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(privateMessageEntities)) {
            return privateMessageEntities.stream().map(ele->transfer(ele)).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }


    private PrivateMessageModel transfer(PrivateMessageEntity entity){
       return   PrivateMessageModel.builder()
                .messageId(entity.getMessageId())
                .cid(entity.getCid())
                .senderId(entity.getSenderId())
                .status(entity.getStatus()).build();
    }


}
